﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;
using DevExpress.XtraEditors.DXErrorProvider;
using System.Threading;

namespace ChinaDynastyTours.Views
{
    public partial class HotelServiceForm : DevExpress.XtraEditors.XtraForm
    {
        private DataTable _dtHotel;
        private bool _isAddStatus = false;

        public HotelServiceForm()
        {
            InitializeComponent();
        }

        private void HotelServiceForm_Load(object sender, EventArgs e)
        {
            //城市信息
            //tbCity.Properties.DataSource = BaseInfoCtrl.GetCityData();
            tbProviderName.Properties.DataSource = BaseInfoCtrl.GetProviderData(new ProviderTypeEnum[] {ProviderTypeEnum.宾馆 });

            InitValidationRules();
            if (Thread.CurrentPrincipal.IsInRole("基础信息管理员") == true)
            {
                btnCancel.Enabled = true;
                btnSave.Enabled = true;
                btnDelete.Enabled = true;
                btnAdd.Enabled = true;
                btnModify.Enabled = true;
            }
            else
            {
                btnCancel.Enabled = false;
                btnSave.Enabled = false;
                btnDelete.Enabled = false;
                btnAdd.Enabled = false;
                btnModify.Enabled = false;

            }
        }

        private void InitUI()
        {
            //tbCity.DataBindings.Clear();
            tbProviderName.DataBindings.Clear();
            tbFeaturesRoom.DataBindings.Clear();
            //tbContractPath.DataBindings.Clear();

            //tbCity.DataBindings.Add("EditValue", _dtHotel, "City");
            tbProviderName.DataBindings.Add("EditValue", _dtHotel, "ProviderID");
            tbFeaturesRoom.DataBindings.Add("EditValue", _dtHotel, "FeaturesRoom");
            //tbContractPath.DataBindings.Add("EditValue", _dtHotel, "ContractPath");
        }

        #region 按钮事件处理

        private void tbContractPath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FilesSelectForm filesSelectForm = new FilesSelectForm();
            if (filesSelectForm.ShowDialog(this) == DialogResult.OK)
            {
                //tbContractPath.EditValue = filesSelectForm.ReturnValues;
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            _dtHotel = ServiceInfoCtrl.GetHotelServiceData();
            this.hotelGridControl.DataSource = _dtHotel;

            InitUI();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            hotelGridView.AddNewRow();

            _isAddStatus = true;
            SetUIElementStatus(true);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (_dtHotel == null) return;

            _isAddStatus = false;

            SetUIElementStatus(true);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!dxValidationProvider.Validate()) return;

            try
            {
                HotelEntity entity = new HotelEntity();
                //entity.City = tbCity.Text;
                entity.ProviderName = tbProviderName.Text;
                entity.ProviderID = tbProviderName.EditValue == null ? -1 : int.Parse(tbProviderName.EditValue.ToString());//lblProviderID.Text == "" ? -1 : int.Parse(lblProviderID.Text.ToString()); 
                entity.FeaturesRoom = tbFeaturesRoom.Text;

                if (_isAddStatus)
                {
                    string HotelID = BaseInfoCtrl.CreateHotelData(entity); ;
                    hotelGridView.SetRowCellValue(hotelGridView.FocusedRowHandle, "HotelID", HotelID);
                }
                else
                {
                    entity.HotelID = int.Parse(hotelGridView.GetRowCellValue(hotelGridView.FocusedRowHandle, "HotelID").ToString());
                    BaseInfoCtrl.UpdateHotelData(entity);
                }
                MessageBox.Show("保存成功！！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存过程出现错误:" + ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                _isAddStatus = false;
                SetUIElementStatus(false);
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnLoad.PerformClick();

            SetUIElementStatus(false);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (hotelGridView.FocusedRowHandle < 0) return;

            if (MessageBox.Show("确定删除选中的汽车服务吗？", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string HotelID = hotelGridView.GetRowCellValue(hotelGridView.FocusedRowHandle, "HotelID").ToString();

                hotelGridView.DeleteRow(hotelGridView.FocusedRowHandle);
                BaseInfoCtrl.DeleteHotelData(HotelID);
            }

        }
        #endregion

        /// <summary>
        /// 控件数据校验
        /// </summary>
        private void InitValidationRules()
        {
            // <notEmptyTextEdit> 
            ConditionValidationRule notEmptyValidationRule = new ConditionValidationRule();
            notEmptyValidationRule.ConditionOperator = ConditionOperator.IsNotBlank;
            notEmptyValidationRule.ErrorText = "不能为空";
            // </notEmptyTextEdit>

            //不能为空
            dxValidationProvider.SetValidationRule(tbFeaturesRoom, notEmptyValidationRule);
            dxValidationProvider.SetValidationRule(tbProviderName, notEmptyValidationRule);
            //dxValidationProvider.SetValidationRule(tbCity, notEmptyValidationRule);

        }


        /// <summary>
        /// 设定界面元素状态
        /// </summary>
        /// <param name="bModifyOrAddMode">设置当前状态为修改或添加状态</param>
        private void SetUIElementStatus(bool bSetToModifyOrAddStatus)
        {
            if (bSetToModifyOrAddStatus == true)
            {
                btnCancel.Visible = true;
                btnSave.Visible = true;
                vehicleGroupPane.Enabled = true;
                hotelGridControl.Enabled = false;
            }
            else
            {
                btnCancel.Visible = false;
                btnSave.Visible = false;
                vehicleGroupPane.Enabled = false;
                hotelGridControl.Enabled = true;
            }
        }

        private void tbProviderName_EditValueChanged(object sender, EventArgs e)
        {
            //根据选择的供应商，刷新gridView中供应商名字
            hotelGridView.SetRowCellValue(hotelGridView.FocusedRowHandle, "ProviderName", tbProviderName.Text);
        }
   }
}
